import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 从文件夹中递归的查询出文件中包含的单词
 */
public class FileSearch3 {
    public static void main(String[] args) {
        String folderPath = "C:\\Users\\ASUS\\Desktop\\慧科项目\\eks-backend\\eks-backend";
        searchFiles(folderPath);
    }

    public static void searchFiles(String folderPath) {
        File folder = new File(folderPath);
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    searchFiles(file.getAbsolutePath());
                } else {
                    searchContent(file);
                }
            }
        }
    }

    public static void searchContent(File file) {
        try {
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;
            int lineNumber = 1;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("elb") || line.contains("3306")) {
                    System.out.println("文件名：" + file.getName());
                    System.out.println("文件路径：" + file.getAbsolutePath());
                    System.out.println("行号：" + lineNumber);
                    System.out.println("行内容：" + line);
                    System.out.println("--------------------------");
                }
                lineNumber++;
            }
            bufferedReader.close();
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
